package logic

import (
	"api/model/mysql"
	"context"
	"fmt"
	"github.com/google/uuid"
	"srv/retaiers"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderaddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewOrderaddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderaddLogic {
	return &OrderaddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *OrderaddLogic) Orderadd(req *types.OrderaddRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	OrderSn := uuid.NewString()
	g := mysql.Goods{}
	err = g.Int(req.GoodId)
	if err != nil {
		return &types.Response{
			Msg:  "下单失败",
			Code: 500,
			Data: nil,
		}, err
	}

	amount := g.GoodsPrice * float64(req.Num)
	fmt.Println(g)
	order, err := l.svcCtx.Reta.AddOrder(l.ctx, &retaiers.AddOrderRequest{
		UserId:  int64(req.UserId),
		GoodsId: int64(req.GoodId),
		Amount:  float32(amount),
		Num:     int64(req.Num),
		OrderSn: OrderSn,
		Payment: req.Payment,
	})
	if err != nil {
		return &types.Response{
			Msg:  "下单失败",
			Code: 500,
			Data: nil,
		}, err
	}
	return &types.Response{
		Msg:  "下单成功",
		Code: 500,
		Data: map[string]interface{}{
			"url": order,
		},
	}, err
}
